package com.tanhua.admin.listener;

import com.tanhua.autoconfig.templates.AliyunGreenTemplate;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class AuditListener {
    @DubboReference
    private MovementApi movementApi;
    @Autowired
    private AliyunGreenTemplate aliyunGreenTemplate;
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "tanhua.audit.queue"),
                    exchange = @Exchange(value = "tanhua.audit.exchange",type = ExchangeTypes.TOPIC),
                    key = "audit.movement"
            )
    )
    public void audit(String id) throws Exception {
        System.out.println("id  = " + id);
        //根据id查询动态内容
        Movement movement = movementApi.findById(id);
        if (movement != null &&movement.getState() ==0){
            //文字
            String textContent = movement.getTextContent();
            //图片
            List<String> medias = movement.getMedias();
            //审核
            Map<String, String> textMap = aliyunGreenTemplate.greenTextScan(textContent);
            Map<String, String> picMap  = aliyunGreenTemplate.imageScan(medias);
            String textSug = textMap.get("suggestion");  //pass：成功
            String picSug= picMap.get("suggestion");
            Integer state = 0; //待审核
            if(textSug.equals("pass") && picSug.equals("pass")) {
                state =1;//已通过
            }else if(textSug.equals("block") || picSug.equals("block")) {
                state =2; //内容违规
            }
            //修改状态
            movementApi.updateState(id,state);
        }



    }
}
